# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.admin.food.canteen import Canteen
from web.models.admin.food.menu import Menu
import time
from lib import libupload

@route(r'/admin/food/menu/?')
class IndexHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        page = (int(cur_page) - 1) * self.offset

        ret, count = yield Menu().select_menu(school_id, page, self.offset)

        page_info = {'total': count, 'page': int(cur_page)}
        self.render("admin/food/menu/list.html", page_info=page_info, data=ret)


@route(r'/admin/food/menu/new/?')
class NewHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        canteen = yield Canteen().select_all_canteen(school_id)

        self.render("admin/food/menu/new.html", canteen=canteen)

    @asynchronous
    @gen.coroutine
    def post(self):
        ltype = self.get_argument('ltype', default='0')
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')
        Fcanteen_id = self.get_argument('Fcanteen_id', default='')
        Fprice = self.get_argument('Fprice', default='')
        Fdesc = self.get_argument('Fdesc', default='')
        Fimg = self.request.files.get('Fimg')

        if Fimg is not None:
            Fimg, path = libupload.save(Fimg[0])
            libupload.img.thumbnail(path, path, 100, 100)
        else:
            Fimg = ""

        menu_info_data = {
            "Fname":         Fname,
            "Fcanteen_id":   Fcanteen_id,
            "Fprice":        Fprice,
            "Fdesc":         Fdesc,
            "Fimg":          Fimg,
            "Fschool_id":    school_id,
            "Fcreate_time":  time.time(),
        }

        count = yield Menu().insert_menu(menu_info_data)

        if ltype == '0':
            self.redirect('/admin/food/menu/')
        else:
            self.redirect('/admin/food/menu/new/')

@route(r'/admin/food/menu/del/(?P<FmenuId>[0-9].*)/(?P<cur_page>[0-9].*)', name='menu.del_menu')
class DelHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, FmenuId, cur_page):
        school_id = self.get_argument('schoolId', default=self.school_id)

        ret = Menu().del_menu(FmenuId)

        self.redirect('/admin/food/menu/?page=' + cur_page)

@route(r'/admin/food/menu/edit/(?P<Fid>[0-9].*)', name='menu.edit')
class EditHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)

        menu = yield Menu().find_menu(Fid)
        canteen = yield Canteen().select_all_canteen(school_id)

        self.render("admin/food/menu/edit.html", menu=menu, canteen=canteen)

    @asynchronous
    @gen.coroutine
    def post(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')
        Fcanteen_id = self.get_argument('Fcanteen_id', default='')
        Fprice = self.get_argument('Fprice', default='')
        Fdesc = self.get_argument('Fdesc', default='')
        Fimg = self.request.files.get('Fimg')

        if Fimg is not None:
            Fimg, path = libupload.save(Fimg[0])
            libupload.img.thumbnail(path, path, 100, 100)
        else:
            Fimg = ""

        menu_info_data = {
            "Fname":         Fname,
            "Fcanteen_id":   Fcanteen_id,
            "Fprice":        Fprice,
            "Fdesc":         Fdesc,
            "Fschool_id":    school_id,
        }
        if Fimg != "":
            menu_info_data['Fimg'] = Fimg

        count = yield Menu().update_menu(Fid, menu_info_data)

        self.redirect('/admin/food/menu/')